//
//  MLAMerchantAuthVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/18.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAMerchantAuthVC.h"
#import <TZImagePickerController.h>
#import "MLABaseFieldView.h"
#import "MLAChoosePhotosView.h"

static NSInteger tag = 10000;

@interface MLAMerchantAuthVC () <TZImagePickerControllerDelegate>

@end

@implementation MLAMerchantAuthVC {
    MLAChoosePhotosView *idCardView;
    MLAChoosePhotosView *businessView;
    MLAChoosePhotosView *repastView;
    NSArray<MLABaseFieldModel *> *_modelsArr; //输入框数据
    
    NSInteger photoViewTag; //0.身份证 1.经营执照 2.餐饮许可证
    
    NSMutableArray *idCardArr;
    NSMutableArray *businessArr;
    NSMutableArray *repastArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTitle:@"商户认证"];
 
    [self addSubviewsInView];
 
    photoViewTag = 0;
    idCardArr = [NSMutableArray array];
    businessArr = [NSMutableArray array];
    repastArr = [NSMutableArray array];
}

#pragma mark - Actions
- (void)commitBtnAction {
    CX_Log(@"登记");
}

- (void)addPhotosDidClick {
    TZImagePickerController *imagePickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVC.allowCrop = YES;
    imagePickerVC.cropRect = CGRectMake(0, (CX_GetScreenHeight()-CX_GetScreenWidth())/2., CX_GetScreenWidth(), CX_GetScreenWidth()*0.74);
    [self presentViewController:imagePickerVC animated:YES completion:nil];
    
    [imagePickerVC setDidFinishPickingPhotosWithInfosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto, NSArray<NSDictionary *> *infos) {
        
    }];
    
    [imagePickerVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (self->photoViewTag == 0) {
            
            [self->idCardArr addObject:photos[0]];
            self->idCardView.imagesArr = [self->idCardArr copy];
        } else if (self->photoViewTag == 1) {
            
            [self->businessArr addObject:photos[0]];
            self->businessView.imagesArr = [self->businessArr copy];
        } else if (self->photoViewTag == 2) {
            
            [self->repastArr addObject:photos[0]];
            self->repastView.imagesArr = [self->repastArr copy];
        }
    }];
}

- (void)deletePhotosDidClickWithDeleteIndex:(NSInteger)index {
    if (photoViewTag == 0) {

        [idCardArr removeObjectAtIndex:index];
        idCardView.imagesArr = [idCardArr copy];
    } else if (photoViewTag == 1) {
        
        [businessArr removeObjectAtIndex:index];
        businessView.imagesArr = [businessArr copy];
    } else if (photoViewTag == 2) {

        [repastArr removeObjectAtIndex:index];
        repastView.imagesArr = [repastArr copy];
    }
}

#pragma mark - Subviews
- (void)addSubviewsInView {
    
    _modelsArr = [self getModelsArr];
    
    CGFloat height = 442+CX_GetXScale(105)*3+20*3+90;
    
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.backgroundColor = CX_GetColorHex(0xFCFCFC, 1);
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), height);
    
    [_modelsArr enumerateObjectsUsingBlock:^(MLABaseFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MLABaseFieldView *fieldView = [[MLABaseFieldView alloc] init];
        fieldView.tag = tag+idx;
        fieldView.model = obj;
        [tableHeaderView addSubview:fieldView];
        
        if (idx == 0) {
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(20);
                make.left.right.equalTo(tableHeaderView);
                make.height.mas_equalTo(85);
            }];
        } else {
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                UIView *view = [tableHeaderView viewWithTag:tag+idx-1];
                make.left.right.height.equalTo(view);
                make.top.equalTo(view.mas_bottom);
            }];
        }
    }];
    
    idCardView = [[MLAChoosePhotosView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewLayout alloc] init]];
    idCardView.title = @"身份证";
    idCardView.viewTag = 0;
    [tableHeaderView addSubview:idCardView];
    [idCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIView *view = [tableHeaderView viewWithTag:tag+self->_modelsArr.count-1];
        make.top.equalTo(view.mas_bottom);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(CX_GetXScale(105));
    }];
    @weakify(self);
    idCardView.photosViewDidClickAdd = ^{
        @strongify(self);
        self->photoViewTag = 0;
        [self addPhotosDidClick];
    };
    idCardView.photosViewDidClickDetele = ^(NSInteger index, NSInteger tag) {
        @strongify(self);
        self->photoViewTag = tag;
        [self deletePhotosDidClickWithDeleteIndex:index];
    };
    
    businessView = [[MLAChoosePhotosView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewLayout alloc] init]];
    businessView.title = @"营业执照：";
    businessView.viewTag = 1;
    [tableHeaderView addSubview:businessView];
    [businessView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->idCardView.mas_bottom).offset(20);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(CX_GetXScale(105));
    }];
    businessView.photosViewDidClickAdd = ^{
        @strongify(self);
        self->photoViewTag = 1;
        [self addPhotosDidClick];
    };
    businessView.photosViewDidClickDetele = ^(NSInteger index, NSInteger tag) {
        @strongify(self);
        self->photoViewTag = tag;
        [self deletePhotosDidClickWithDeleteIndex:index];
    };

    repastView = [[MLAChoosePhotosView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewLayout alloc] init]];
    repastView.title = @"餐饮经营许可证：";
    repastView.viewTag = 2;
    [tableHeaderView addSubview:repastView];
    [repastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->businessView.mas_bottom).offset(20);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(CX_GetXScale(105));
    }];
    repastView.photosViewDidClickAdd = ^{
        @strongify(self);
        self->photoViewTag = 2;
        [self addPhotosDidClick];
    };
    repastView.photosViewDidClickDetele = ^(NSInteger index, NSInteger tag) {
        @strongify(self);
        self->photoViewTag = tag;
        [self deletePhotosDidClickWithDeleteIndex:index];
    };

    UIButton *commitBtn = [self getCommitButtonWithTitle:@"登记"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
        make.top.equalTo(self->repastView.mas_bottom).offset(30);
    }];
    
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(CX_GetScreenWidth());
        make.height.mas_equalTo(height);
    }];

}

- (NSArray<MLABaseFieldModel *> *)getModelsArr {
    NSArray *infosArr = @[CX_GetFieldInfoDic(@"商户号", @"", @"30", @"0", @"0", @"0", @"Q008"),
                          CX_GetFieldInfoDic(@"负责人", @"", @"30", @"0", @"0", @"0", @"张三"),
                          CX_GetFieldInfoDic(@"手机号2", @"额外补充...", @"11", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"手机号3", @"额外补充...", @"11", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"电子邮箱", @"额外补充...", @"30", @"0", @"1", @"0", @"")];
    return [[MLABaseFieldModel mj_objectArrayWithKeyValuesArray:infosArr] copy];
}


@end
